package com.ymatou.iapi.quickturnover.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.quickturnover.parameter.ApplyAdvanceBillBean;
import com.ymatou.iapi.quickturnover.parameter.CreateAdvanceOrderReqBean;
import com.ymatou.iapi.quickturnover.parameter.GetSellerAdvanceInfoReqBean;
import com.ymatou.iapi.quickturnover.parameter.GrantCreditReqBean;
import com.ymatou.iapi.quickturnover.service.AdvanceOrderService;
import com.ymatou.iapi.quickturnover.service.ApplyAdvanceBillCall;
import com.ymatou.iapi.quickturnover.service.CreateAdvanceOrderReqCall;
import com.ymatou.iapi.quickturnover.service.GetSellerAdvanceInfoReqCall;
import com.ymatou.iapi.quickturnover.service.GrantCreditReqCall;
import com.ymatou.operate.trading.api.parameter.DispatchedBean;
import com.ymatou.operate.trading.api.service.DispatchedCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.enums.BillTypeEnum;
import com.ymttest.database.model.AdvanceApplyBill;
import com.ymttest.database.model.AdvanceOrder;
import com.ymttest.database.model.YmtAccountEntry;
import com.ymttest.database.model.YmtSettlementInvoice;
import com.ymttest.database.sqlwapper.AccountDefaultWapper;
import com.ymttest.database.sqlwapper.AdvanceOrderDefaultWapper;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtSettlementInvoiceWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*创建提前放款批次接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ApplyAdvanceBill{
	private static ApplyAdvanceBillBean applyadvancebillBean;
	private static ApplyAdvanceBillCall applyadvancebillCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建提前放款批次接口");
		AdvanceOrderService.applyAdvanceAllBill();
	}

	@Before
	public void caseUp() {
		applyadvancebillBean=new ApplyAdvanceBillBean();
		applyadvancebillCall=new ApplyAdvanceBillCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(applyadvancebillCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verifyDB(String billId){
		
		try {
			AdvanceOrderDefaultWapper advanceOrderDefaultWapper=new AdvanceOrderDefaultWapper();	
	        DefaultWapper defaultWapper = new DefaultWapper();	
			//advance_order
	        Map advance = new HashMap();  
	        advance.put("bill_id", billId);
	        List<AdvanceOrder> advanceOrderList=advanceOrderDefaultWapper.selectAdvanceOrder(advance);
			//advance_apply_bill
	        List<AdvanceApplyBill>advanceApplyBillList=advanceOrderDefaultWapper.selectAdvanceApplyBil(billId);		
	        // 查询结算表
			List<YmtSettlementInvoice> ymtSettlementInvoiceList=new YmtSettlementInvoiceWapper().selectYmtSettlementInvoice(billId);
			//资金流水表
			Map act = new HashMap();      
			int sellerId=applyadvancebillBean.getSellerId();
	        act.put("userId", sellerId);         
	        act.put("originalNo", billId);
	        List<YmtAccountEntry> ymtAccountEntryList= new AccountDefaultWapper().selectAccountEntryByBizNo(act);
	        
	        Logger.comment("校验advance_order表");
	              	
        	BigDecimal total_advanced_amount=advanceOrderList.stream().map(advanceOrder->advanceOrder.getAdvanced_amount()).reduce(BigDecimal.ZERO,BigDecimal::add);
			Logger.verifyEquals(applyadvancebillBean.getAdvanceAmount().setScale(2), total_advanced_amount, "验证advanced_amount");
			advanceOrderList.forEach(advanceOrder->
        		{	   			
        			Logger.verifyEquals(true, advanceOrder.getCan_advance_amount().compareTo(advanceOrder.getAdvanced_amount())>-1, "验证advanced_amount小于最大可申请金额");
	        		Logger.verifyEquals(3, advanceOrder.getAdvance_status(), "验证advance_status");
	        		Logger.verifyEquals(sellerId, advanceOrder.getSeller_id(), "验证seller_id");
	        		Logger.verifyEquals(billId, advanceOrder.getBill_id(), "验证bill_id");
        		}		
			 );
	       
	        Logger.comment("校验advance_apply_bill表");
	        advanceApplyBillList.forEach(advanceApplyBill->
	        			{
			        		Logger.verifyEquals(billId, advanceApplyBill.getBill_id(), "验证bill_id");
			        		Logger.verifyEquals(applyadvancebillBean.getAdvanceAmount().setScale(2), advanceApplyBill.getAdvance_amount(), "验证advance_amount");
			        		Logger.verifyEquals(sellerId, advanceApplyBill.getSeller_id(), "验证seller_id");
			        		Logger.verifyEquals(1, advanceApplyBill.getLoan_status(), "验证loan_status");
	        			}
	        		);  
	        Logger.comment("校验Ymt_SettlementInvoice表");
    		Logger.verifyEquals(2, ymtSettlementInvoiceList.size(), "验证结算Ymt_SettlementInvoice 查询记录为2");
    		Logger.comment("businessType=10");
	        ymtSettlementInvoiceList.stream().filter(ymtSettlementInvoice->ymtSettlementInvoice.getBusinessType()==10).forEach(ymtSettlementInvoice->
	        			{	        				
			        		Logger.verifyEquals(applyadvancebillBean.getAdvanceAmount().setScale(2), ymtSettlementInvoice.getTotalAmount(), "验证结算TotalAmount");
			        		Logger.verifyEquals(applyadvancebillBean.getAdvanceAmount().setScale(2), ymtSettlementInvoice.getCashAmount(), "验证结算CashAmount");
			        		Logger.verifyEquals(sellerId, ymtSettlementInvoice.getSellerId(), "验证结算商家sellerid");
			        		Logger.verifyEquals("预支申请", ymtSettlementInvoice.getRemark(), "验证结算remark");
			        		Logger.verifyEquals(1, ymtSettlementInvoice.getSettlementStatus(), "验证结算SettlementStatus");
			        		Logger.verifyEquals(2, ymtSettlementInvoice.getSettlementUser(), "验证结算SettlementUser");

	        			}
	        		);  
    		Logger.comment("businessType=11");
	        ymtSettlementInvoiceList.stream().filter(ymtSettlementInvoice->ymtSettlementInvoice.getBusinessType()==11).forEach(ymtSettlementInvoice->
			{	        				
        		Logger.verifyEquals(applyadvancebillBean.getServiceFee().setScale(2), ymtSettlementInvoice.getTotalAmount(), "验证结算TotalAmount");
        		Logger.verifyEquals(applyadvancebillBean.getServiceFee().setScale(2), ymtSettlementInvoice.getCashAmount(), "验证结算CashAmount");
        		Logger.verifyEquals(sellerId, ymtSettlementInvoice.getSellerId(), "验证结算商家sellerid");
        		Logger.verifyEquals("扣除预支服务费", ymtSettlementInvoice.getRemark(), "验证结算remark");
        		Logger.verifyEquals(1, ymtSettlementInvoice.getSettlementStatus(), "验证结算SettlementStatus");
        		Logger.verifyEquals(2, ymtSettlementInvoice.getSettlementUser(), "验证结算SettlementUser");

			}
		);  
	        Logger.comment("校验Ymt_AccountEntry表");
    		Logger.verifyEquals(2, ymtAccountEntryList.size(), "验证结算Ymt_AccountEntry 查询记录为2");
    		Logger.comment("bizcode=300031");
	        ymtAccountEntryList.stream().filter(ymtAccountEntry->ymtAccountEntry.getBizcode()==300031).forEach(ymtAccountEntry->
	        			{
			        		Logger.verifyEquals(applyadvancebillBean.getAdvanceAmount().setScale(2), ymtAccountEntry.getAmount(), "验证Amount");
			        		Logger.verifyEquals(sellerId, ymtAccountEntry.getUserid(), "验证UserId");
			        		Logger.verifyEquals("预支申请", ymtAccountEntry.getMemo(), "验证Memo");
	        			}
	        		);   
    		Logger.comment("bizcode=300032");
	        ymtAccountEntryList.stream().filter(ymtAccountEntry->ymtAccountEntry.getBizcode()==300032).forEach(ymtAccountEntry->
			{
        		Logger.verifyEquals(applyadvancebillBean.getServiceFee().divide(new BigDecimal(-1),2,4), ymtAccountEntry.getAmount(), "验证Amount");
        		Logger.verifyEquals(sellerId, ymtAccountEntry.getUserid(), "验证UserId");
        		Logger.verifyEquals("扣除预支服务费", ymtAccountEntry.getMemo(), "验证Memo");
			}
		);   
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyAdvanceBill_001() {
		Logger.start(true,"单个订单申请提前放款成功");
		try {
			Map map=AdvanceOrderService.createAdvanceOrderAndGrantcredit();
	        int sellerId = (Integer) map.get("sellerId");
			applyadvancebillBean.setSellerId(sellerId);
			applyadvancebillBean.setAdvanceAmount(new BigDecimal(2));
			applyadvancebillBean.setServiceFee(BigDecimal.ONE);
			applyadvancebillCall.setData(applyadvancebillBean);
			applyadvancebillCall.callService();			
			Thread.sleep(2000);
			Logger.verifyEquals(ECode.SUCCESS, applyadvancebillCall.getCode(), "验证Code");		
			Logger.verifyEquals(true, applyadvancebillCall.getData().getBoolean("success"), "验证success");		
			if (applyadvancebillCall.getCode().equals("200")){
				String billId=applyadvancebillCall.getData().getString("billId");			
				verifyDB(billId);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyAdvanceBill_002() {
		Logger.start(true,"申请金额大于授信金额");
		try {	
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			BigDecimal can_advanced_amount=AdvanceOrderService.getSellerAdvanceinforeq(sellerId);
			applyadvancebillBean.setSellerId(sellerId);
			applyadvancebillBean.setAdvanceAmount(can_advanced_amount.add(BigDecimal.ONE));
			applyadvancebillBean.setServiceFee(BigDecimal.ONE);
			applyadvancebillCall.setData(applyadvancebillBean);
			applyadvancebillCall.callService();		
			Thread.sleep(2000);
			Logger.verifyEquals(ECode.FAILED201, applyadvancebillCall.getCode(), "验证Code");		
			Logger.verifyEquals("当前买手"+sellerId+"申请预支金额大于可预支金额", applyadvancebillCall.getString("msg"), "验证Code");		
			Logger.verifyEquals(false, applyadvancebillCall.getData().getBoolean("success"), "验证success");		

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyAdvanceBill_003() {
		Logger.start(true,"多个订单申请最大可放款金额，校验每个订单上分摊的放款金额");
		try {
			Map map=AdvanceOrderService.createAdvanceOrderAndGrantcredit();
			AdvanceOrderService.createAdvanceOrderAndGrantcredit();
			AdvanceOrderService.createAdvanceOrderAndGrantcredit();
	        int sellerId = (Integer) map.get("sellerId");
	        
			BigDecimal can_advanced_amount=AdvanceOrderService.getSellerAdvanceinforeq(sellerId);
			
			applyadvancebillBean.setSellerId(sellerId);
			applyadvancebillBean.setAdvanceAmount(can_advanced_amount);
			applyadvancebillBean.setServiceFee(BigDecimal.ONE);
			applyadvancebillCall.setData(applyadvancebillBean);
			applyadvancebillCall.callService();		
			Thread.sleep(2000);
			Logger.verifyEquals(ECode.SUCCESS, applyadvancebillCall.getCode(), "验证Code");		
			Logger.verifyEquals(true, applyadvancebillCall.getData().getBoolean("success"), "验证success");		
			if (applyadvancebillCall.getCode().equals("200")){
				String billId=applyadvancebillCall.getData().getString("billId");			
				verifyDB(billId);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyAdvanceBill_004() {
		Logger.start(true,"多个订单申请部分预支金额，校验每个订单上分摊的放款金额");
		try {
			Map map=AdvanceOrderService.createAdvanceOrderAndGrantcredit();
			AdvanceOrderService.createAdvanceOrderAndGrantcredit();
			AdvanceOrderService.createAdvanceOrderAndGrantcredit();
	        int sellerId = (Integer) map.get("sellerId");
	        
			BigDecimal can_advanced_amount=AdvanceOrderService.getSellerAdvanceinforeq(sellerId);
			
			applyadvancebillBean.setSellerId(sellerId);
			applyadvancebillBean.setAdvanceAmount(can_advanced_amount);
			applyadvancebillBean.setServiceFee(BigDecimal.ONE);
			applyadvancebillCall.setData(applyadvancebillBean);
			applyadvancebillCall.callService();		
			Thread.sleep(2000);
			Logger.verifyEquals(ECode.SUCCESS, applyadvancebillCall.getCode(), "验证Code");		
			Logger.verifyEquals(true, applyadvancebillCall.getData().getBoolean("success"), "验证success");		
			if (applyadvancebillCall.getCode().equals("200")){
				String billId=applyadvancebillCall.getData().getString("billId");			
				verifyDB(billId);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyAdvanceBill_005() {
		Logger.start(true,"sellid 不存在");
		try {	
			int sellerId = 1111;				
			applyadvancebillBean.setSellerId(sellerId);
			applyadvancebillBean.setAdvanceAmount(BigDecimal.ONE);
			applyadvancebillBean.setServiceFee(BigDecimal.ONE);
			applyadvancebillCall.setData(applyadvancebillBean);
			applyadvancebillCall.callService();			
			Logger.verifyEquals(ECode.FAILED201, applyadvancebillCall.getCode(), "验证Code");		
			Logger.verifyEquals("当前买手"+sellerId+"申请预支金额大于可预支金额", applyadvancebillCall.getString("msg"), "验证Code");		
			Logger.verifyEquals(false, applyadvancebillCall.getData().getBoolean("success"), "验证success");		

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyAdvanceBill_006() {
		Logger.start(true,"sellid 为null");
		try {	
			int sellerId = 1111;				
			//applyadvancebillBean.setSellerId(sellerId);
			applyadvancebillBean.setAdvanceAmount(BigDecimal.ONE);
			applyadvancebillBean.setServiceFee(BigDecimal.ONE);
			applyadvancebillCall.setData(applyadvancebillBean);
			applyadvancebillCall.callService();			
			Logger.verifyEquals(ECode.ERROR400, applyadvancebillCall.getCode(), "验证Code");		
			Logger.verifyEquals("sellerId:sellerId不能为空", applyadvancebillCall.getString("msg"), "验证Code");		
			Logger.verifyEquals(false, applyadvancebillCall.getData().getBoolean("success"), "验证success");		

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyAdvanceBill_007() {
		Logger.start(true,"预支金额为0");
		try {	
			//Map map=AdvanceOrderService.createAdvanceOrderAndGrantcredit();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			applyadvancebillBean.setSellerId(sellerId);
			applyadvancebillBean.setAdvanceAmount(BigDecimal.ZERO);
			applyadvancebillBean.setServiceFee(BigDecimal.ONE);
			applyadvancebillCall.setData(applyadvancebillBean);
			applyadvancebillCall.callService();			
			Logger.verifyEquals(ECode.ERROR400, applyadvancebillCall.getCode(), "验证Code");		
			Logger.verifyEquals("advanceAmount:advanceAmount未提供", applyadvancebillCall.getString("msg"), "验证Code");		
			Logger.verifyEquals(false, applyadvancebillCall.getData().getBoolean("success"), "验证success");		

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyAdvanceBill_008() {
		Logger.start(true,"预支金额小于0");
		try {	
			//Map map=AdvanceOrderService.createAdvanceOrderAndGrantcredit();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			applyadvancebillBean.setSellerId(sellerId);
			applyadvancebillBean.setAdvanceAmount(new BigDecimal(-1));
			applyadvancebillBean.setServiceFee(BigDecimal.ONE);
			applyadvancebillCall.setData(applyadvancebillBean);
			applyadvancebillCall.callService();			
			Logger.verifyEquals(ECode.ERROR400, applyadvancebillCall.getCode(), "验证Code");		
			Logger.verifyEquals("advanceAmount:advanceAmount未提供", applyadvancebillCall.getString("msg"), "验证Code");		
			Logger.verifyEquals(false, applyadvancebillCall.getData().getBoolean("success"), "验证success");		

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyAdvanceBill_009() {
		Logger.start(true,"预支结算失败，校验结算补单逻辑");
		try {
			Map map=AdvanceOrderService.createAdvanceOrderAndGrantcredit();
	        int sellerId = (Integer) map.get("sellerId");
			applyadvancebillBean.setSellerId(sellerId);
			applyadvancebillBean.setAdvanceAmount(new BigDecimal(2));
			applyadvancebillBean.setServiceFee(BigDecimal.ONE);
			applyadvancebillCall.setData(applyadvancebillBean);
			applyadvancebillCall.callService();			
			Thread.sleep(2000);
			Logger.verifyEquals(ECode.SUCCESS, applyadvancebillCall.getCode(), "验证Code");		
			Logger.verifyEquals(true, applyadvancebillCall.getData().getBoolean("success"), "验证success");		
			if (applyadvancebillCall.getCode().equals("200")){
				String billId=applyadvancebillCall.getData().getString("billId");			
				verifyDB(billId);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

}